package net.gz01.wallet.web.controller.api.sdk.client.authForfadada;

import com.alibaba.fastjson.JSON;
import net.gz01.util.ZOLog;
import net.gz01.wallet.web.controller.api.sdk.client.authForfadada.model.AgentInfoINO;
import net.gz01.wallet.web.controller.api.sdk.client.authForfadada.model.BankInfoINO;
import net.gz01.wallet.web.controller.api.sdk.client.authForfadada.model.CompanyInfoINO;
import net.gz01.wallet.web.controller.api.sdk.client.authForfadada.model.LegalInfoINO;
import net.gz01.wallet.web.controller.api.sdk.client.common.FddClient;
import net.gz01.wallet.web.controller.api.sdk.util.crypt.MsgDigestUtil;
import net.gz01.wallet.web.controller.api.sdk.util.http.ClientMultipartFormPost;
import net.gz01.wallet.web.controller.api.sdk.util.http.HttpsUtil;

import java.util.HashMap;
import java.util.Map;

public class GetCompanyVerifyUrl extends FddClient {
    public GetCompanyVerifyUrl(String appId, String secret, String version, String url) {
        super(appId, secret, version, url);
    }

    /**
     * 获取企业实名认证的接口地址
     * @return
     */
    public String getCompanyVerifyUrl() {
        return super.getUrl() + "get_company_verify_url.api";
    }

    /**
     *  获取企业实名认证地址接口
     *  @date: 2018年12月23日
     * @param companyInfo 企业信息
     * @param bankInfo  银行信息
     * @param legalInfo 法人信息
     * @param agentInfo 代理人信息
     * @param customer_id 客户编号 注册账号时返回
     * @param verified_way 实名认证套餐类型
     * @param page_modify 是否允许用户页面修改
     * @param company_principal_type 企业负责人身份:1.法人，2代理人
     * @param return_url 同步通知url
     * @param notify_url 回调地址
     * @return
     */
    public String invokeCompanyVerifyUrl(CompanyInfoINO companyInfo, BankInfoINO bankInfo,
                                         LegalInfoINO legalInfo, AgentInfoINO agentInfo,
                                         String customer_id, String verified_way,
                                         String page_modify, String company_principal_type,
                                         String return_url, String notify_url) {
        Map<String,String> params = new HashMap<String,String>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            params.put("customer_id", customer_id);
            params.put("verified_way", verified_way);
            params.put("page_modify", page_modify);
            params.put("notify_url", notify_url);
            params.put("return_url", return_url);
            params.put("company_principal_type",company_principal_type);

            if(null != companyInfo){
                String companyInfojson = JSON.toJSONString(companyInfo);
                params.put("company_info",companyInfojson);
            }
            if(null != bankInfo){
                String bankInfojson = JSON.toJSONString(bankInfo);
                params.put("bank_info",bankInfojson);
            }
            if(null != legalInfo){
                String legalInfojson = JSON.toJSONString(legalInfo);
                params.put("legal_info",legalInfojson);
            }
            if(null != companyInfo){
                String agentInfojson = JSON.toJSONString(agentInfo);
                params.put("agent_info",agentInfojson);
            }

            String[] sortforParameters= MsgDigestUtil.sortforParameters(params);
            msgDigest =MsgDigestUtil.getCheckMsgDigest(
                    super.getAppId(),super.getSecret(),
                    timeStamp, sortforParameters);
            params.put("app_id",super.getAppId());
            params.put("timestamp",timeStamp);
            params.put("v",super.getVersion());
            params.put("msg_digest",msgDigest);

            ZOLog.api.info("FDD -->获取企业实名接口上送:" + params.toString());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return ClientMultipartFormPost.doPost(this.getCompanyVerifyUrl(), params,null);
    }
}
